package com.ar.engine;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
//import android.util.Log;

// Here sensors and user input are handled and passed to the renderer
public class EngineGLSurfaceView extends GLSurfaceView implements SensorEventListener{
	
	EngineRenderer engineRenderer;
	
	private float accelX;
	private float accelY;
	private float accelZ;
	
//	private float orientationX;
//	private float orientationY;
//	private float orientationZ;

//	Sensor magnetField;
	
	public EngineGLSurfaceView(Context context, EngineRenderer renderer) {
		super(context);
		
		setEGLContextClientVersion(2);
		
		//Passing our render engine to the surface
//		engineRenderer = new EngineRenderer(context);
//		this.setRenderer(engineRenderer);
		
		
		engineRenderer = renderer;
		
		
		//Getting accelerometer sensor
		SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0)
		{
			Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
			manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
			
			//Check for the compass as well !
//			magnetField = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
//			manager.registerListener(this, magnetField, SensorManager.SENSOR_DELAY_GAME);
		}
	}
	
//	SensorEventListener magnetListener = new SensorEventListener() {
//		
//		public void onSensorChanged(SensorEvent event) {
//			orientationX = event.values[0];
//			orientationY = event.values[1];
//			orientationZ = event.values[2];
//			
//			engineRenderer.setRotation(orientationX, orientationY, orientationZ);
//		}
//		
//		public void onAccuracyChanged(Sensor sensor, int accuracy) {
//			
//		}
//	};

//	private float[] mR = new float[16];
//
//	private float[] mI = new float[16];
//
//	private float[] mGData = new float[3];
//
//	private float[] mMData = new float[3];
//
//	private float[] mOrientation = new float[3];
//
//	private int mCount;

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Not needed at the current moment	
	}

	public void onSensorChanged(SensorEvent event) {
		
		//High Pass Filter
//		filtered x = accelX - (accelX * 0.65) + (filtered x * (1.0 - 0.65));
//		filtered y = accelY - (accelY * 0.65) + (filtered y * (1.0 - 0.65));
//		filtered z = accelZ - (accelZ * 0.65) + (filtered z * (1.0 - 0.65));
		
		//Applying low pass filter to smooth the oscillation of the accelerometer
		
//		accelX = (float) ((event.values[0] * 0.1) + (accelX * (1.0 - 0.1)));
//		accelY = (float) ((event.values[1] * 0.1) + (accelY * (1.0 - 0.1)));
//		accelZ = (float) ((event.values[2] * 0.1) + (accelZ * (1.0 - 0.1)));
		
		//Applying high pass filter to smooth the oscillation of the accelerometer
		
		accelX = (float) (event.values[0] - (event.values[0] * 0.1) + (accelX * (1.0 - 0.1)));
		accelY = (float) (event.values[1] - (event.values[1] * 0.1) + (accelY * (1.0 - 0.1)));
		accelZ = (float) (event.values[2] - (event.values[2] * 0.1) + (accelZ * (1.0 - 0.1)));
		
		//Passing raw data!
//		accelX = event.values[0];
//		accelY = event.values[1];
//		accelZ = event.values[2];
		
//		engineRenderer.setColor(accelX, accelY, accelZ);
		engineRenderer.setRotation(accelX, accelY, accelZ);
		
		
//		int type = event.sensor.getType();
//        float[] data;
//        if (type == Sensor.TYPE_ACCELEROMETER) {
//            data = mGData;
//        } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
//            data = mMData;
//        } else {
//            // we should not be here.
//            return;
//        }
//        for (int i=0 ; i<3 ; i++)
//            data[i] = event.values[i];
//
//        SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
//        
//        SensorManager.getOrientation(mR, mOrientation);
//        
//        float incl = SensorManager.getInclination(mI);
//
//        if (mCount++ > 1) {
//            final float rad2deg = (float)(180.0f/Math.PI);
//            mCount = 0;
////            Log.d("Compass", 
////            		  "yaw: " + (int)(mOrientation[0]*rad2deg) +
////                    "  pitch: " + (int)(mOrientation[1]*rad2deg) +
////                    "  roll: " + (int)(mOrientation[2]*rad2deg) +
////                    "  incl: " + (int)(incl*rad2deg)
////                    );
//            
//            orientationX = (float) ((mOrientation[0]*rad2deg) - ((mOrientation[0]*rad2deg) * 0.1) + (orientationX * (1.0 - 0.1)));
//            orientationY = (float) ((mOrientation[1]*rad2deg) - ((mOrientation[1]*rad2deg) * 0.1) + (orientationY * (1.0 - 0.1)));
//            orientationZ = (float) ((mOrientation[2]*rad2deg) - ((mOrientation[2]*rad2deg) * 0.1) + (orientationZ * (1.0 - 0.1)));
            
            
//            engineRenderer.setRotation(orientationX, orientationY, orientationZ);
            
        }
		
		
}
